package com.medicine.hbp.sp;

import com.google.gson.Gson;
import com.medicine.hbp.adapter.ClassificationDetailAdapter;
import com.medicine.hbp.adapter.FragmentAdapter03;
import com.medicine.hbp.adapter.HomeAdapter;
import com.medicine.hbp.mob.artGroupListVo;
import com.medicine.hbp.mob.laberInfoListVo;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

public class DadosSp {
	private final static String MAIN = "main";
	private final static String HOME = "home";
	private final static String HOME_SIZE = "home_size";
	private static SharedPreferences sharedPreferences;
	private static SharedPreferences.Editor editor;

	/**
	 * sy页面数据缓存
	 *
	 * @param str
	 */
	public static void setTabOne(Context context, String str) {
		sharedPreferences = context.getSharedPreferences(MAIN, -1);
		editor = sharedPreferences.edit();
		editor.putString(HOME, str);
		editor.commit();
		// Log.e("settttttttt", str);
	}

	public static String getTabOne(Context context) {
		sharedPreferences = context.getSharedPreferences(MAIN, -1);
		return sharedPreferences.getString(HOME, "");
	}

	/**
	 * 读取缓存数据并显示
	 *
	 * @param adapter
	 */
	public static void setAdapterOne(Context context, HomeAdapter adapter) {
		Gson gson = new Gson();
		if (!TextUtils.isEmpty(getTabOne(context))) {
			laberInfoListVo result = gson.fromJson(getTabOne(context),
					laberInfoListVo.class);
			adapter.changeData(result.laberInfo);
		}
	}

	/**
	 * sy页面数据总数缓存
	 *
	 * @param str
	 */
	public static void setHomeSzie(Context context, String str) {
		sharedPreferences = context.getSharedPreferences(MAIN, -1);
		editor = sharedPreferences.edit();
		editor.putString(HOME_SIZE, str);
		editor.commit();
		// Log.e("settttttttt", str);
	}

	public static String getHomeSzie(Context context) {
		sharedPreferences = context.getSharedPreferences(MAIN, -1);
		return sharedPreferences.getString(HOME_SIZE, "");
	}

	private final static String CLASSIFICATION_DETAIL = "ClassificationDetail";
	/**
	 * 文章页面数据缓存
	 * @param str
	 */
	public static void setClassificationDetail(Context context,String id, String str) {
		sharedPreferences = context.getSharedPreferences(MAIN, -1);
		editor = sharedPreferences.edit();
		editor.putString(CLASSIFICATION_DETAIL+id, str);
		editor.commit();
	}
	public static String getClassificationDetail(Context context,String id) {
		sharedPreferences = context.getSharedPreferences(MAIN, -1);
		return sharedPreferences.getString(CLASSIFICATION_DETAIL+id, "");
	}
	/**
	 * 读取缓存数据并显示
	 * @param adapter
	 */
	public static void setAdapter(Context context,String id, ClassificationDetailAdapter adapter) {
		Gson gson = new Gson();
		if (!TextUtils.isEmpty(getClassificationDetail(context,id))) {
			artGroupListVo result = gson.fromJson(getClassificationDetail(context,id),artGroupListVo.class);
			adapter.changeData(result.artInfo);
		}
	}



	private final static String ART_GROUP_LIST_VO = "art_group_list_vo";
	/**
	 * 收藏页面数据缓存
	 * @param str
	 */
	public static void setartGroupListVo(Context context,String id, String str) {
		sharedPreferences = context.getSharedPreferences(MAIN, -1);
		editor = sharedPreferences.edit();
		editor.putString(ART_GROUP_LIST_VO+id, str);
		editor.commit();
	}
	public static String getartGroupListVo(Context context,String id) {
		sharedPreferences = context.getSharedPreferences(MAIN, -1);
		return sharedPreferences.getString(ART_GROUP_LIST_VO+id, "");
	}
	/**
	 * 读取缓存数据并显示
	 * @param adapter
	 */
	public static void setAdapter(Context context,String id, FragmentAdapter03 adapter) {
		Gson gson = new Gson();
		if (!TextUtils.isEmpty(getClassificationDetail(context,id))) {
			artGroupListVo result = gson.fromJson(getClassificationDetail(context,id),artGroupListVo.class);
			adapter.changeData(result.artInfo);
		}
	}
	//-------------------------------------进入也跳转缓存--------------------------------------
	private final static String SPLASH_IMG = "splash_img";
	private final static String SPLASH_URL = "splash_url";
	/**
	 *进入也跳转图片地址缓存
	 * @param str
	 */
	public static void setSplashImg(Context context, String str) {
		sharedPreferences = context.getSharedPreferences(MAIN, -1);
		editor = sharedPreferences.edit();
		editor.putString(SPLASH_IMG, str);
		editor.commit();
	}
	public static String getSplashImg(Context context) {
		sharedPreferences = context.getSharedPreferences(MAIN, -1);
		return sharedPreferences.getString(SPLASH_IMG, "");
	}

	/**
	 * 进入也跳转地址缓存
	 * @param str
	 */
	public static void setSplashUrl(Context context, String str) {
		sharedPreferences = context.getSharedPreferences(MAIN, -1);
		editor = sharedPreferences.edit();
		editor.putString(SPLASH_URL, str);
		editor.commit();
	}
	public static String getSplashUrl(Context context) {
		sharedPreferences = context.getSharedPreferences(MAIN, -1);
		return sharedPreferences.getString(SPLASH_URL, "");
	}
}
